package com.hanxun.model;

import java.security.PublicKey;

/**
 * @Author: zzq
 * @Data:2024-02-06 1:48
 */

/**
 * 建造者模式
 */
public class User {
    private Integer id;
    private String name;
    private String address;

    private User() {
        System.out.println("开始调用User自己的无参构造");
    }


    private User(User origin) {
        System.out.println("开始调用User自己的有参构造");
        this.id = origin.id;
        this.name = origin.name;
        this.address = origin.address;
    }


    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }


    public static User.Builder builder(){
        return new User.Builder();
    }


    public static class Builder {
        private User target;

        public Builder() {//静态类的构造方法
            System.out.println("我调用了User.Builder的构造方法？");
            this.target = new User();
        }

        public Builder id(Integer id) {
            target.id = id;
            System.out.println("利用Builder进行了赋值id的工作");
            return this;
        }

        public Builder name(String name) {
            target.name = name;
            System.out.println("利用Builder进行了赋值name的工作");
            return this;
        }

        public Builder address(String address) {
            target.address = address;
            System.out.println("利用Builder进行了赋值address的工作");
            return this;
        }

        public User build() {
            System.out.println("完成构造，返回User对象，使用User自己的有参构造方法创建User对象");
            return new User(target);
        }
    }
}
